﻿using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Collections.ObjectModel;

namespace BlogTruyen.Models
{
    public class Story : BaseModel
    {
        public string full_title { get; set; }
        public string title { get; set; }
        public string chapter_count { get; set; }
        public string summary { get; set; }
        public string description { get; set; }
        public string update_date { get; set; }
        public string cover_img { get; set; }
        public string url { get; set; }
        public string view_count { get; set; }
        public string like_count { get; set; }
        public string artist { get; set; }
        public ObservableCollection<Chapter> chapters { get; set; }
        public string genre { get; set; }

        public bool isLoaded { get; set; }
        public Story()
        {
            chapters = new ObservableCollection<Chapter>();
        }

        public void LoadHtml(string html)
        {
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(html);
            var section = doc.DocumentNode.SelectSingleNode("//section[@class='bg-white story-detail']");
            var titleNode = section.SelectSingleNode("./h1[@class='entry-title']").SelectSingleNode("./a");
            var imgNode = section.SelectSingleNode("./div[@class='thumbnail']").SelectSingleNode("./img");
            var descNode = section.SelectSingleNode("./div[@class='description']");
            var pDescNodes = descNode.SelectNodes("./p");

            full_title = HttpUtility.HtmlDecode(titleNode.InnerText).Trim();
            cover_img = imgNode.GetAttributeValue("src", "");
            genre = "";
            foreach (HtmlNode pNode in pDescNodes)
            {
                if (pNode.InnerText.Contains("Thể loại"))
                {
                    var spanNodes = pNode.SelectNodes("./span");
                    foreach (var spanNode in spanNodes)
                    {
                        genre += ", " + HttpUtility.HtmlDecode(spanNode.InnerText).Trim();
                    }
                }
                else if (pNode.InnerText.Contains("Tác giả"))
                {
                    artist = HttpUtility.HtmlDecode(pNode.SelectSingleNode("./a").InnerText).Trim();
                }
                else if (pNode.InnerText.Contains("Update"))
                {
                    update_date = pNode.SelectSingleNode("./span").InnerText.Trim();
                }
            }
            //view_count = descNode.SelectSingleNode(".//span[@id='PageViews']").InnerText;
            //like_count = descNode.SelectSingleNode(".//span[@id='LikeCount']").InnerText;
            genre = genre.Substring(2);
            var regex = new Regex(@"(?<count>\d+) CHƯƠNG");
            var match = regex.Match(html);
            chapter_count = match.Groups["count"].ToString();
            var listChapterNode = doc.DocumentNode.SelectSingleNode("//div[@id='list-chapters']");
            var pChapterNodes = listChapterNode.SelectNodes("./p");
            foreach (HtmlNode pChapterNode in pChapterNodes)
            {
                var aNode = pChapterNode.SelectSingleNode(".//a");
                chapters.Add(new Chapter
                {
                    name = HttpUtility.HtmlDecode(aNode.InnerText).Trim().Substring(title.Length+1).Trim(),
                    url = aNode.GetAttributeValue("href",""),
                    upload_date = pChapterNode.SelectSingleNode("./span[@class='publishedDate']").InnerText,
                });
            }
            summary = HttpUtility.HtmlDecode(section.SelectSingleNode(".//div[@class='content']").InnerText).Trim();
            isLoaded = true;

            NotifyPropertyChanged("title");
            NotifyPropertyChanged("full_title");
            NotifyPropertyChanged("cover_img");
            NotifyPropertyChanged("genre");
            NotifyPropertyChanged("artist");
            NotifyPropertyChanged("chapter_count");
            NotifyPropertyChanged("summary");
            NotifyPropertyChanged("update_date");
            NotifyPropertyChanged("isLoaded");
            //NotifyPropertyChanged("view_count");
            //NotifyPropertyChanged("like_count");
            
        }
    }
}
